﻿using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace Assets.Scripts.TDuResource
{
    public class TDuClipInfoReader
    {
        private XmlDocument m_xmlDocument;
        protected string m_strClipCfgPath;  //切割配置文件;
        public  AnimationClips m_clipSections;  //模型所包含的动画

        public bool m_bEnableClip; //是否允许切割动画

        public TDuClipInfoReader(string strClipCfgPath)
        {
            m_strClipCfgPath = strClipCfgPath;
            m_xmlDocument = new XmlDocument();
            m_clipSections = new AnimationClips();
        }

        #region 加载切割配置文件
        public bool loadClipCfgFile()
        {
            if (m_strClipCfgPath.Length <= 0)
            {
                Debug.LogError("Clip Cfg File is empty!");
                return false;
            }

            string _path = m_strClipCfgPath;
            if (!File.Exists(_path))
                return false;

            XmlReaderSettings _set = new XmlReaderSettings();
            _set.IgnoreComments = true;

            XmlReader _reader = XmlReader.Create(_path, _set);
            m_xmlDocument.Load(_reader);
            _reader.Close();

            return true;
        }
        #endregion

        #region 读取切割段落信息;
        private bool readClipSections()
        {
            XmlNodeList _nodeList = m_xmlDocument.SelectSingleNode("Root").ChildNodes;
            foreach (XmlElement _xe in _nodeList)
            {
                if (_xe.Name == "ModelFbx")
                {
                    foreach (XmlElement _x1 in _xe.ChildNodes)
                    {
                        if (_x1.Name == "ModelName")
                        {
                            m_clipSections.m_strModelName = _x1.InnerText;
                        }
                        if (_x1.Name == "Clips")
                        {
                            foreach (XmlElement _x2 in _x1.ChildNodes)
                            {
                                string _clipName = "";
                                int _firstFrame = 0;
                                int _lastFrame = 0;
                                bool _isLoop = false;
                                foreach (XmlElement _x3 in _x2.ChildNodes)
                                {
                                    if (_x3.Name == "ClipName")
                                    {
                                        _clipName = _x3.InnerText;
                                    }
                                    if (_x3.Name == "FirstFrame")
                                    {
                                        _firstFrame = int.Parse(_x3.InnerText);
                                    }
                                    if (_x3.Name == "LastFrame")
                                    {
                                        _lastFrame = int.Parse(_x3.InnerText);
                                    }
                                    if (_x3.Name == "IsLoop")
                                    {
                                        _isLoop = bool.Parse(_x3.InnerText);
                                    }
                                }

                                m_clipSections.m_ltClipInfos.Add(new AnimationClipInfo(_clipName, _firstFrame, _lastFrame, _isLoop));
                            }
                        }
                    }
                }
            }

            return true;
        }
        #endregion

        #region 读取切割开关;
        private bool readClipEnabled()
        {
            XmlNodeList _nodeList = m_xmlDocument.SelectSingleNode("Root").ChildNodes;
            foreach (XmlElement _xe in _nodeList)
            {
                if (_xe.Name == "Enable")
                {
                    m_bEnableClip = bool.Parse(_xe.InnerText);
                    break;
                }
            }

            return true;
        }
        #endregion

        #region 读取器
        public bool reader()
        {
            bool bRes = false;

            bRes = loadClipCfgFile();
            bRes = readClipEnabled();
            bRes = readClipSections();

            return bRes;
        }

        #endregion

    }

}
